﻿using System;
using System.Collections.Generic;
using Missbot.Service;

namespace Missbot.WeiboBot
{
    /// <summary>
    /// 微博博文记录
    /// </summary>
    public class WeiboStatusRec : DbBaseEntity
    {
        /// <summary>
        /// 机器人名
        /// </summary>
        public string BotName { get; init; }
        
        /// <summary>
        /// 博文ID
        /// </summary>
        public string StatusID { get; init; }

        /// <summary>
        /// 博文正文
        /// </summary>
        public string Text { get; init; }

        /// <summary>
        /// 博文附件
        /// </summary>
        public List<string> Files { get; init; } = new();

        /// <summary>
        /// 记录创建时间
        /// </summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;
        
        /// <summary>
        /// 微博发布时间
        /// </summary>
        public DateTime PubTime { get; set; }
        
        /// <summary>
        /// 是否发送过嘟文
        /// </summary>
        public bool IsTooted { get; set; }
        
        /// <summary>
        /// 是否忽略这条记录
        /// </summary>
        public bool IsIgnored { get; init; }
    }

    public static class WeiboStatusRecDA
    {
        /// <summary>
        /// 插入
        /// </summary>
        public static void Insert(this WeiboStatusRec entity)
        {
            MongoService<WeiboStatusRec>.Insert(entity);
        }

        /// <summary>
        /// 获取指定机器人负责的未被发送过博文
        /// </summary>
        /// <param name="BotName"></param>
        /// <returns></returns>
        public static WeiboStatusRec GetStatus(string BotName)
        {
            return MongoService<WeiboStatusRec>.GetOnly(p => p.BotName == BotName && p.IsTooted == false && p.IsIgnored == false, p => p.CreateTime);
        }

        /// <summary>
        /// 更新
        /// </summary>
        public static void Update(this WeiboStatusRec entity)
        {
            MongoService<WeiboStatusRec>.Update(entity);
        }

        /// <summary>
        /// 根据博文ID判断博文是否存在
        /// </summary>
        /// <param name="StatusID"></param>
        /// <returns></returns>
        public static bool IsExist(string StatusID)
        {
            return MongoService<WeiboStatusRec>.Any(p => p.StatusID == StatusID);
        }
    }
}